package com.lanyou.esb.cook.proxy.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.lanyou.esb.cook.proxy.dao.impl.BaseDao;
import com.lanyou.esb.cook.proxy.entity.AccLog;
import com.lanyou.esb.cook.proxy.redis.CommonUtils;
import com.lanyou.esb.cook.proxy.redis.proxy.impl.AccLogJedisProxy;
import com.lanyou.esb.cook.proxy.redis.vo.Group;
import com.lanyou.esb.cook.proxy.redis.vo.Group.Entry;
import com.lanyou.esb.cook.proxy.service.AccLogService;
import com.lanyou.esb.cook.proxy.service.GroupService;
import com.lanyou.esb.cook.proxy.service.Service;
import com.lanyou.esb.cook.proxy.service.push.Push;

/**
 * 服务日志
 * 
 * @author Davey.wu
 */
public class AccLogServiceImpl implements AccLogService, GroupService<AccLog>,
		Service<AccLog> {
	/** 数据库操作接口 **/
	private BaseDao<AccLog> dao;
	/** Redis代理接口 **/
	private AccLogJedisProxy jedisProxy;
	/** 消息推送接口 **/
	private Push<AccLog> push;

	@Override
	public Group getGroup(AccLog log) {
		if (log == null) {
			return null;
		}
		String json = "";
		try {
			json = CommonUtils.toJson(log);
		} catch (Exception e) {
			return null;
		}
		Group group = new Group();
		// list类型条件集合
		List<Entry> lists = new ArrayList<>(1);
		group.setLists(lists);

		Entry entry = new Entry();
		entry.setKey(jedisProxy.getKey());
		entry.setValue(json);
		lists.add(entry);
		return group;
	}

	/**
	 * 将日志存储到Redis
	 *
	 * @param log
	 */
	@Override
	public void pushToRedis(AccLog log) {
		try {
			// 保存日志到Redis
			jedisProxy.rpush(jedisProxy.getKeyFromObject(log), log);
		} catch (Exception e) {
			// 保存失败，将日志加入消息队列，用定时任务去尝试推送到REDIS
			push.putToCollection4Redis(log);
		}

	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void save(final List<AccLog> list) throws Exception {
		if (list == null || list.isEmpty()) {
			return;
		}

		try {
			TransactionTemplate tt = new TransactionTemplate(
					this.dao.getTransactionManager());
			tt.execute(new TransactionCallback() {
				public Object doInTransaction(TransactionStatus status) {
					return AccLogServiceImpl.this.dao.saveAll(list);
				}
			});
		} catch (Exception e) {
			// 保留失败，将记录添加到推送队列中
			push.putToCollection4DB(list);
		}
	}

	@Override
	public void update(List<AccLog> list) throws Exception {
		// 日志目前不存在更新业务，所以不提供实现
	}

	public AccLogJedisProxy getJedisProxy() {
		return jedisProxy;
	}

	public void setJedisProxy(AccLogJedisProxy jedisProxy) {
		this.jedisProxy = jedisProxy;
	}

	public BaseDao<AccLog> getDao() {
		return dao;
	}

	public void setDao(BaseDao<AccLog> dao) {
		this.dao = dao;
	}

	public Push<AccLog> getPush() {
		return push;
	}

	public void setPush(Push<AccLog> push) {
		this.push = push;
	}

}
